using System;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using DevTools.HardDiskVisualizer.Components;

namespace DevTools.HardDiskVisualizer.Forms
{
    public partial class HardDiskVisualizerForm : RootForm
    {
        public HardDiskVisualizerForm()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (fbdFolder.ShowDialog() == DialogResult.OK)
                txeFolder.Text = fbdFolder.SelectedPath;

            Enabled = false;
            bdsFolder.Clear();
            bgwWorker.RunWorkerAsync(FolderList.Instance);
        }

        private void bgwWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            FolderList list = (FolderList) e.Argument;

            list.ScanBeginning += (o, args) => bgwWorker.ReportProgress(0, args.Folder);

            list.Scan(txeFolder.Text);
            list.CalculateSum();
        }

        private void bgwWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Folder folder = (Folder) e.UserState;

            briStatus.Caption = folder.Path;
        }

        private void bgwWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Enabled = true;

            briStatus.Caption = string.Empty;

            bdsFolder.DataSource = FolderList.Instance;
        }

        private void trlList_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            Folder folder = (Folder) trlList.GetDataRecordByNode(e.Node);

            if (folder == null)
                return;

            uscChartViewer.SetDataSource(folder.Children);
        }
    }
}